<?php

namespace Core\Library;

use Phalcon\Mailer\Manager;
use Phalcon\Di;
use Modules\Queue\Library\Queue;
use Modules\Queue\Models\Queue as QueueModel;

/**
 * Class Email
 * @package Core\Library
 */
class Email
{
    public static function send($toMail, $name, $title, $body, $cc = null, $bcc = null)
    {
        $baseConfig = Di::getDefault()->getShared('config')->get('m.core.email');

        $config = [
            'driver' => 'mail',
            'from' => [
                'email' => 'leno@itdashu.com',
                'name' => 'IT大叔'
            ]
        ];
        switch ($baseConfig['driver']) {
            case 'smtp':
                $config = [
                    'driver' => 'smtp',
                    'host' => $baseConfig['smtp_host'],
                    'port' => $baseConfig['smtp_port'],
                    'encryption' => $baseConfig['smtp_encryption'],
                    'username' => $baseConfig['smtp_username'],
                    'password' => $baseConfig['smtp_password'],
                    'from' => [
                        'email' => $baseConfig['smtp_from_email'],
                        'name' => $baseConfig['smtp_from_name']
                    ]
                ];
                break;
            case 'mail':
                $config = [
                    'driver' => 'mail',
                    'from' => [
                        'email' => $baseConfig['mail_from_email'],
                        'name' => $baseConfig['mail_from_name']
                    ]
                ];
                break;
            case 'sendmail':
                $config = [
                    'driver' => 'sendmail',
                    'sendmail' => $baseConfig['sendmail'],
                    'from' => [
                        'email' => $baseConfig['sendmail_from_email'],
                        'name' => $baseConfig['sendmail_from_name']
                    ]
                ];
                break;
        }

        $mailer = new Manager($config);

        $message = $mailer->createMessage()
            ->to($toMail, $name)
            ->subject($title)
            ->content($body);

        if (!is_null($cc)) {
            $message->cc($cc);
        }

        if (!is_null($bcc)) {
            $message->bcc($bcc);
        }

        $output = $message->send();
        return $output;
    }

    public static function addQueue($param)
    {
        $queue = new Queue();
        $queue->add('imgToLocal', $param);
    }

    public static function queueSend(QueueModel $queue)
    {
        $queue->isRun();
        $data = objectToArray($queue->data);
        $total = count($data);
        $i = 1;
        foreach ($data as $key => $item) {
            $queue->percentage($i, $total);
            $i++;
            self::send($item['toMail'], $item['name'], $item['title'], $item['body'], $item['cc'], $item['bcc']);
        }
        if (empty($data)) {
            return true;
        }
        $queue->isRun(false);
        return $data;
    }
}
